package org.j4me.deviceinfo.data;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import org.dom4j.Element;

public class Brand {
	
	Hashtable<String, Model> _models = new Hashtable<String, Model>();
	
	private String _name;
	
	public Brand(String brand) {
	_name = brand;
}
	
	public Brand(Element elt) {
		
		_name = elt.attributeValue("name");
		Iterator it = elt.selectNodes("model").iterator();
		while (it.hasNext()) {
			Model model = new Model(this, (Element) it.next());
			addModel(model);
			
			
		}
	}


	public Enumeration<Model> getModels() {
		return _models.elements();
	}

	public Enumeration<String> getModelNames() {
		return _models.keys();
	}

	public Model getModel(String modelName) {
		return _models.get(modelName);
	}

	public void addDevice(Device device) {
		Model model = getModel(device.getModel());
		if (model == null)
		{
			model = new Model(device.getModel());
			_models.put(model.getName(), model);
		}
		model.addDevice(device);

		
	}

	public void addModel(Model model) {
	
		Model existing = getModel(model.getName());
		if (existing == null)
			_models.put(model.getName(), model);
		else
			existing.merge(model);
		
	}

	public String getName() {
		return _name;
	}

	public void addXmlNode(Element root) {
		Element brand = root.addElement("brand");
		brand.addAttribute("name", _name);
		Enumeration<Model> models = getModels();
		while (models.hasMoreElements()) {
			Model model = (Model) models.nextElement();
			model.addXmlNode(brand);
		}
		
	}

	public int getModelCount() {
		return _models.size();
	}

	int getDeviceCount()
	{
		int count = 0;
		Enumeration<Model> models = getModels();
		while (models.hasMoreElements()) {
			count += models.nextElement().getDeviceCount();
			
		}
		return count;
	}
	int getHitCount()
	{
		int count = 0;
		Enumeration<Model> models = getModels();
		while (models.hasMoreElements()) {
			count += models.nextElement().getHitCount();
			
		}
		return count;
	}
}
